<template>
  <!--企业资料新增弹层start -->
  <el-dialog
    v-dialogDrag
    :title="dialogTitle"
    :close-on-click-modal="false"
    :visible.sync="dialogVisible"
    class="dialogStyleSmall"
    :before-close="cancel"
  >
    <el-form
      ref="addForm"
      class="dialog-form"
      :model="formData"
      :rules="rules"
      label-width="80px"
    >
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="资料名称" prop="fileName">
            <el-input v-model="formData.fileName" placeholder="请输入资料名称" size="medium" />
          </el-form-item>
        </el-col>
        <!--        <el-col :span="24">-->
        <!--          <el-form-item label="上传人" prop="createBy">-->
        <!--            <label slot="label" v-html="`上&ensp;传&ensp;人`" />-->
        <!--            <el-input v-model="formData.createBy" placeholder="请输入资料上传人" size="medium" />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="24">
          <el-form-item label="附件" prop="appFiles">
            <template slot="label">
              <span class="labelTitle" v-html="`附&emsp;&emsp;件`" />
            </template>
            <FileUplodeOne
              ref="fileUplode"
              file-type=".pdf"
              :read-only="false"
              :edit-file-list="FileList"
              @getFileIdsList="getFileList"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <DialogFooter />
    </div>
  </el-dialog>
  <!--试验单位信息新增弹层end -->
</template>

<script>
// 页面接口
import { addEnterpriseData, editEnterpriseData, detailEnterpriseData } from '@/api/system/enterpriseData'
// 附件上传
import FileUplodeOne from '@/components/Upload/FileUplodeOne'
// dialog-footer
import DialogFooter from '@/components/Common/Btns/DialogFooter/index'
export default {
  components: {
    FileUplodeOne,
    DialogFooter
  },
  data() {
    return {
      /* 新增 start*/
      dialogTitle: '', // 弹层标题
      dialogVisible: false, // 弹层是否显示
      btnDisabled: false, // 确定按钮
      formData: { // 新增数据
        id: '',
        fileName: '', // 规范名称
        createBy: '', // 规范编号
        appFiles: ''// 附件
      },
      FileList: [], // 附件
      // 表单验证规则
      rules: {
        fileName: [
          { required: true, message: '资料名称不能为空', pattern: '[^\x22]+', trigger: 'blur' }
        ],
        createBy: [
          { required: true, message: '上传人不能为空', trigger: 'blur' }
        ],
        appFiles: [
          { required: true, message: '请上传附件', trigger: 'blur' }
        ]
      }
      /* 新增 end*/

    }
  },
  methods: {
    /* 新增/编辑弹层 start*/
    // 新增/编辑
    handleAdd(title, id) {
      this.dialogTitle = title
      this.dialogVisible = true
      this.emptyAddForm() // 清空勘察单位
      this.formData.id = id
      if (id) {
        this.getDetailById(id)
      }
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false
      this.btnDisabled = false
      this.emptyAddForm()
    },
    // 提交按钮
    submitForm() {
      this.$refs['addForm'].validate(valid => {
        if (valid) {
          this.btnDisabled = true
          if (this.formData.id) {
            // 更新
            editEnterpriseData(this.formData).then(res => {
              if (res) {
                this.msgSuccess('修改成功')
                this.$parent.getList()
                this.cancel()
              } else {
                this.msgError(res.message)
                this.btnDisabled = false
              }
            })
          } else {
            addEnterpriseData(this.formData).then(res => {
              if (res) {
                this.msgSuccess('保存成功')
                this.$parent.getList()
                this.cancel()
              } else {
                this.msgError(res.message)
                this.btnDisabled = false
              }
            })
          }
        }
      })
    },
    // 获取详情
    getDetailById(id) {
      detailEnterpriseData(id).then(res => {
        if (res) {
          this.formData = { ...res.data }
          if (res.data.appFiles && res.data.appFiles.length > 0) {
            this.FileList = res.data.appFiles
          }
        }
      })
    },
    // 表单重置
    emptyAddForm() {
      this.resetForm('addForm')
      this.formData.id = ''
      this.FileList = []
    },
    /* 新增弹层 end*/

    /** ************ 文件上传start *******************/
    // 接受文件上传 组件 回传过来的文件id
    getFileList(fileIdsList) {
      this.formData.appFiles = fileIdsList
    }
    /** ************ 文件上传end *******************/
  }

}
</script>

<style>

</style>
